package a1;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;

public class Frame extends JFrame{
    JButton shang;//定义按钮
    JButton xia;
    JButton zuo;
    JButton you;
    JButton qiuzhu;
    JButton chongzhi;
    JPanel imagePanel;
    //定义二维数组，存储图片编号
    int[][] datas = {
            {1, 2, 3, 4},
            {5, 6, 7, 8},
            {9, 10, 11, 12},
            {13, 14, 15, 0}
    };

    //定义成功数组
    int[][] winDatas = {
            {1, 2, 3, 4},
            {5, 6, 7, 8},
            {9, 10, 11, 12},
            {13, 14, 15, 0}
    };
    //记录零号图片索引
    int x0,y0;

    //无参构造方法
    public Frame() {
        //窗体的基本设置
        initFrame();
        //二维数组元素打乱
        initData();
        //窗体上组件的绘制
        paintView();
        //给按钮注册事件
        addButtonEvent();
        //设置窗体可见
        this.setVisible(true);
    }


    //窗体基本设置
    public void initFrame(){
        this.setSize(960,565);
        this.setTitle("动漫拼图");
        this.setLocationRelativeTo(null);
        this.setDefaultCloseOperation(3);
        this.setAlwaysOnTop(true);
        this.setLayout(null);
    }
    public void paintView(){
        JLabel title=new JLabel(new ImageIcon("pintu1\\images\\title.png"));
        title.setBounds(354, 27, 232, 57);
        this.add(title);

        imagePanel =new JPanel();
        imagePanel.setBounds(150,114,360,360);
        imagePanel.setLayout(null);

        for (int i = 0; i < datas.length; i++) {
            for (int j = 0; j < datas[i].length; j++) {
                JLabel imageLable=new JLabel(new ImageIcon("pintu1\\images\\"+datas[i][j]+".png"));
                imageLable.setBounds(j*90,i*90,90,90);
                imagePanel.add(imageLable);
            }

        }
        this.add(imagePanel);

        //参照图
        JLabel canzhaotuLabel=new JLabel(new ImageIcon("pintu1\\images\\canzhaotu.png"));
        canzhaotuLabel.setBounds(574,114,122,121);
        this.add(canzhaotuLabel);
        //上下左右按钮
        shang = new JButton(new ImageIcon("pintu1\\images\\shang.png"));
        shang.setBounds(732, 265, 57, 57);
        this.add(shang);

        zuo = new JButton(new ImageIcon("pintu1\\images\\zuo.png"));
        zuo.setBounds(650, 347, 57, 57);
        this.add(zuo);

        xia = new JButton(new ImageIcon("pintu1\\images\\xia.png"));
        xia.setBounds(732, 347, 57, 57);
        this.add(xia);

        you = new JButton(new ImageIcon("pintu1\\images\\you.png"));
        you.setBounds(813, 347, 57, 57);
        this.add(you);

        //求助和重置按钮
        qiuzhu = new JButton(new ImageIcon("pintu1\\images\\qiuzhu.png"));
        qiuzhu.setBounds(626, 444, 108, 45);
        this.add(qiuzhu);

        chongzhi = new JButton(new ImageIcon("pintu1\\images\\chongzhi.png"));
        chongzhi.setBounds(786, 444, 108, 45);
        this.add(chongzhi);

        //背景图
        JLabel backgroundLabel = new JLabel(new ImageIcon("pintu1\\images\\background.png"));
        backgroundLabel.setBounds(0, 0, 960, 530);
        this.add(backgroundLabel);
    }
//二维数组打乱
    public void initData(){
        Random r=new Random();
        for (int i = 0; i < datas.length; i++) {
            for (int j = 0; j < datas[i].length; j++) {
                int x=r.nextInt(datas.length);
                int y=r.nextInt(datas[x].length);

                int temp=datas[i][j];

                datas[i][j]=datas[x][y];
                datas[x][y]=temp;

            }

        }
        //定位0号空白图片的位置
        wc:
        for (int i = 0; i < datas.length; i++) {
            for (int j = 0; j < datas[i].length; j++) {
                if(datas[i][j]==0){
                    x0=i;
                    y0=j;
                    break wc;
                }
            }
        }
    }
    public void rePaintView(){
        //一处容器上组件
        imagePanel.removeAll();

        imagePanel=new JPanel();
        imagePanel.setBounds(150,114,360,360);
        imagePanel.setLayout(null);

        for (int i = 0; i < datas.length; i++) {
            for (int j = 0; j < datas[i].length; j++) {
                JLabel imageLabel = new JLabel(new ImageIcon("pintu1\\images\\" + datas[i][j] + ".png"));
                imageLabel.setBounds(j * 90, i * 90, 90, 90);
                imagePanel.add(imageLabel);
            }
        }
        this.add(imagePanel);

        //重新绘制
        imagePanel.repaint();
    }

    public void success(){
        datas = new int[][]{
                {1, 2, 3, 4},
                {5, 6, 7, 8},
                {9, 10, 11, 12},
                {13, 14, 15, 16}
        };
        shang.setEnabled(false);
        zuo.setEnabled(false);
        xia.setEnabled(false);
        you.setEnabled(false);
    }
    public boolean issuccess(){
        for (int i = 0; i < datas.length; i++) {
            for (int j = 0; j < datas[i].length; j++) {
                if(datas[i][j]!=winDatas[i][j]){
                    return false;
                }
            }
        }
        return true;
    }
    //给按钮注册事件
    public void addButtonEvent(){
        shang.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                if(x0==3){
                    return;
                }
                //元素交换
                datas[x0][y0]=datas[x0+1][y0];
                datas[x0+1][y0]=0;
                x0=x0+1;
                //判断移动是否成功
                if(issuccess()){
                    success();
                }
                //重新绘制图形
                rePaintView();
            }
        });
        zuo.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                //边界处理
                if (y0 == 3) {
                    return;
                }

                //元素交换
                datas[x0][y0] = datas[x0][y0 + 1];
                datas[x0][y0 + 1] = 0;
                y0 = y0 + 1;

                //判断移动是否成功
                if(issuccess()) {
                    success();
                }

                //重新绘制图形
                rePaintView();
            }
        });
        xia.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                //边界处理
                if (x0 == 0) {
                    return;
                }

                //元素交换
                datas[x0][y0] = datas[x0 - 1][y0];
                datas[x0 - 1][y0] = 0;
                x0 = x0 - 1;

                //判断移动是否成功
                if(issuccess()) {
                    success();
                }

                //重新绘制图形
                rePaintView();
            }
        });
        you.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                //边界处理
                if (y0 == 0) {
                    return;
                }

                //元素交换
                datas[x0][y0] = datas[x0][y0 - 1];
                datas[x0][y0 - 1] = 0;
                y0 = y0 - 1;

                //判断移动是否成功
                if(issuccess()) {
                    success();
                }

                //重新绘制图形
                rePaintView();
            }
        });
        qiuzhu.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                success();
                rePaintView();
            }
        });
        chongzhi.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                datas = new int[][]{
                        {1,2,3,4},
                        {5,6,7,8},
                        {9,10,11,12},
                        {13,14,15,0}
                };
                initData();
                rePaintView();
                shang.setEnabled(true);
                zuo.setEnabled(true);
                xia.setEnabled(true);
                you.setEnabled(true);
            }
        });



    }
}


